package com.hotel.front;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.NoteTitleLogic;

import data.NoteTitle;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class NoteTitleService extends HttpServlet{
	//http://localhost:8080/notetitleService?func=addNoteTitle&title=1&description=aa
	//http://localhost:8080/notetitleService?func=editNoteTitle&notetitleId=880&title=12345&description=aabbb
	//http://localhost:8080/notetitleService?func=delNoteTitle&notetitleId=880
	//http://localhost:8080/notetitleService?func=changeStatus&notetitleId=880
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("addNoteTitle")){
				AddNoteTitle(req,resp);
			}else if(doClass.equals("editNoteTitle")){
				EditNoteTitle(req,resp);
			}else if(doClass.equals("delNoteTitle")){
				DeleteNoteTitle(req,resp);
			}else if(doClass.equals("changeStatus")){
				ChangeStatus(req,resp);
			}else if(doClass.equals("GetAllNote4Admin")){
				GetAllNote4Admin(req,resp);
			}else if(doClass.equals("getNoteById")){
				GetNoteById(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void AddNoteTitle(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}	
		long hotelId = (Long) session.getAttribute("hotelId");
		String title = req.getParameter("title");
		String description = req.getParameter("description");

		new NoteTitleLogic().AddNoteTitle(hotelId,title,description);
		
		GetAllNote4Admin(req, resp);
	}

	private void EditNoteTitle(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long notetitleId = Long.parseLong(req.getParameter("notetitleId"));
		String title = req.getParameter("title");
		String description = req.getParameter("description");
				
		new NoteTitleLogic().EditNoteTitle(notetitleId,title,description);
		
		GetAllNote4Admin(req, resp);

	}

	private void DeleteNoteTitle(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String notetitleId = req.getParameter("notetitleId");
		
		for(int i = 0; i < notetitleId.split(",").length ; i++){
			new NoteTitleLogic().DeleteNoteTitle(Long.parseLong(notetitleId.split(",")[i]));
		}
		
		GetAllNote4Admin(req, resp);
	}
	
	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long notetitleId = Long.parseLong(req.getParameter("notetitleId"));

		Object obj = new NoteTitleLogic().ChangeStatus(notetitleId);

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetAllNote4Admin(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		
		List<NoteTitle> obj = new NoteTitleLogic().GetAllNoteTitle4AdminLimit(hotelId,offset,line);
		double dataSize = new NoteTitleLogic().GetAllNoteTitle4Admin(hotelId).size();
		
		Gson gson = new Gson();
		
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", obj);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetNoteById(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		NoteTitle note = new NoteTitleLogic().GetNoteTitleById(id);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(note));
	}
}
